﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GuiBuilder
{
    public partial class Form1 : Form
    {
        System.IO.StreamWriter fUIinc;
        System.IO.StreamWriter fUIsrc;

        String StrParent, StrChild, StrNext, StrPrev;

        public Form1()
        {
            InitializeComponent();
        }

        private void PrintControslFromPanel(Panel pnl, int x, int y)
        {
            int i;

            for (i = 0; i < pnl.Controls.Count; i++)
            {
                if (pnl.Controls[i] is Panel)
                {
                    Console.WriteLine("// PANEL{0}", i);
                    Panel cntrl = (Panel)pnl.Controls[i];
                    PrintControslFromPanel((Panel)pnl.Controls[i], cntrl.Left, cntrl.Top);
                }
            }

            //extern declarations for ui.h
            for (i = 0; i < pnl.Controls.Count; i++)
            {
                //#define		WIDGET_IMAGE
                if (pnl.Controls[i] is PictureBox)
                {
                    PictureBox cntrl = (PictureBox)pnl.Controls[i];
                    fUIinc.WriteLine("extern struct sWidgetImage {0};", cntrl.Name);
                }
                //#define		WIDGET_TEXT
                else if (pnl.Controls[i] is Label)
                {
                    Label cntrl = (Label)pnl.Controls[i];
                    fUIinc.WriteLine("extern struct sWidgetText {0};", cntrl.Name);
                }
                //#define		WIDGET_BUTTON
                else if (pnl.Controls[i] is Button)
                {
                    Button cntrl = (Button)pnl.Controls[i];
                    fUIinc.WriteLine("extern void {0}CallBackPress( struct sWidgetButton *pButton );", cntrl.Name);
                    fUIinc.WriteLine("extern void {0}CallBackHold( struct sWidgetButton *pButton );", cntrl.Name);
                    fUIinc.WriteLine("extern void {0}CallBackRelease( struct sWidgetButton *pButton );", cntrl.Name);
                    fUIinc.WriteLine("extern struct sWidgetButton {0};", cntrl.Name);
                }
            }

            //struct definition for middle of ui.c
            for (i = 0; i < pnl.Controls.Count; i++)
            {
                if (i - 1 >= 0)
                {
                    StrPrev = "&" + pnl.Controls[i - 1].Name + ".widget";
                }
                else
                {
                    StrPrev = "NULL";
                }
                if (i + 1 < pnl.Controls.Count)
                {
                    StrNext = "&" + pnl.Controls[i + 1].Name + ".widget";
                }
                else
                {
                    StrNext = "NULL";
                }
            
                //#define		WIDGET_IMAGE		0x0001
                if (pnl.Controls[i] is PictureBox)
                {
                    PictureBox cntrl = (PictureBox)pnl.Controls[i];
                    fUIsrc.WriteLine("struct sWidgetImage {0} = {{", cntrl.Name);
                    fUIsrc.WriteLine("	{");
                    fUIsrc.WriteLine("		WIDGET_ID_IMAGE,");
                    fUIsrc.WriteLine("		WIDGET_FL_BOX,");
                    fUIsrc.WriteLine("		0,");
                    fUIsrc.WriteLine("		{{ {0}, {1}, {2}, {3} }},", x + cntrl.Left, y + cntrl.Top, cntrl.Width, cntrl.Height);
                    fUIsrc.WriteLine("		&myGLCD,");
                    fUIsrc.WriteLine("		{0}, {1}, {2}, {3}", StrParent, StrChild, StrNext, StrPrev );
                    fUIsrc.WriteLine("	},");
                    fUIsrc.WriteLine("	NULL,");
                    fUIsrc.WriteLine("};");
                }
                //#define		WIDGET_TEXT			0x0002
                else if (pnl.Controls[i] is Label)
                {
                    Label cntrl = (Label)pnl.Controls[i];
                    fUIsrc.WriteLine("struct sWidgetText {0} = {{", cntrl.Name );
                    fUIsrc.WriteLine("	{");
                    fUIsrc.WriteLine("		WIDGET_ID_TEXT,");
                    fUIsrc.WriteLine("		WIDGET_FL_BOX,");
                    fUIsrc.WriteLine("		0,");
                    fUIsrc.WriteLine("		{{ {0}, {1}, {2}, {3} }},", x + cntrl.Left, y + cntrl.Top, cntrl.Width, cntrl.Height);
                    fUIsrc.WriteLine("		&myGLCD,");
                    fUIsrc.WriteLine("		{0}, {1}, {2}, {3}", StrParent, StrChild, StrNext, StrPrev);
                    fUIsrc.WriteLine("	},");
                    fUIsrc.WriteLine("	&{0},", cntrl.Font.Name);
                    fUIsrc.WriteLine("	\"{0}\"", cntrl.Text);
                    fUIsrc.WriteLine("};");

                }
                //#define		WIDGET_BUTTON		0x0005
                else if (pnl.Controls[i] is Button)
                {
                    Button cntrl = (Button)pnl.Controls[i];
                    fUIsrc.WriteLine("struct sWidgetButton {0} = {{", cntrl.Name);
                    fUIsrc.WriteLine("	{");
                    fUIsrc.WriteLine("		WIDGET_ID_BUTTON,");
                    fUIsrc.WriteLine("		WIDGET_FL_BOX,");
                    fUIsrc.WriteLine("		0,");
                    fUIsrc.WriteLine("		{{ {0}, {1}, {2}, {3} }},", x + cntrl.Left, y + cntrl.Top, cntrl.Width, cntrl.Height);
                    fUIsrc.WriteLine("		&myGLCD,");
                    fUIsrc.WriteLine("		{0}, {1}, {2}, {3}", StrParent, StrChild, StrNext, StrPrev);
                    fUIsrc.WriteLine("	},");
                    fUIsrc.WriteLine("	&{0},", cntrl.Font.Name);
                    fUIsrc.WriteLine("	\"{0}\",", cntrl.Text);
                    fUIsrc.WriteLine("	NULL,");
                    fUIsrc.WriteLine("	NULL/*{0}CallBackPress*/,", cntrl.Name);
                    fUIsrc.WriteLine("	NULL/*{0}CallBackHold*/,", cntrl.Name);
                    fUIsrc.WriteLine("	NULL/*{0}CallBackRelease*/,", cntrl.Name);
                    fUIsrc.WriteLine("};");

                }

            }


            return;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int i;

            fUIinc = new System.IO.StreamWriter(".\\UIinc.txt");
            fUIsrc = new System.IO.StreamWriter(".\\UIsrc.txt");
            
            for (i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i] is Panel)
                {
                    if (i - 1 >= 0)
                    {
                        StrParent = "&" + this.Controls[i - 1].Name + ".widget";
                    }
                    else
                    {
                        StrParent = "NULL";
                    }
                    if (i + 1 < this.Controls.Count)
                    {
                        StrChild = "&" + this.Controls[i + 1].Name + ".widget";
                    }
                    else
                    {
                        StrChild = "NULL";
                    }


                    Console.WriteLine("// SCREEN{0}", i);

                    fUIinc.WriteLine("// SCREEN{0}", i);
                    fUIsrc.WriteLine("// SCREEN{0}", i);                  
                    PrintControslFromPanel((Panel)this.Controls[i], 0, 0);
                }
            }
            
            fUIinc.Close();
            fUIsrc.Close();
            
            return;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
}
